package aad.storyboard.view;

import jxl.format.Orientation;
import utils.ViewUtils;
import aad.storyboard.R;
import aad.storyboard.controller.MenuTouchListener;
import aad.storyboard.controller.MenuViewProvider;
import aad.storyboard.model.Story;
import aad.storyboard.model.menu.Menu;
import aad.storyboard.model.menu.MenuItem;
import aad.storyboard.model.menu.MenuSettings;
import aad.storyboard.model.menu.PlainCentralizedMenuBuilder;
import aad.storyboard.view.StoryBar;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MenuViewCreator{
        
        private static MenuViewCreator instance = new MenuViewCreator();
        private MenuTouchListener listener;
        private Activity cont;
        
        
        private MenuViewCreator(){
                
        }

        public static MenuViewCreator getInstance(Activity c){
                MenuViewProvider provider = MenuViewProvider.getInstance();
                instance.cont = c;
                instance.listener = MenuTouchListener.getInstance(c, provider);
                return instance;
        }
        
        /**
         * takes a menu as parameter, and creates a menuview out of it, such that model (menu) and view (menuview) are completely separated
         */
        public View createMenuView(Menu menu){

            RelativeLayout menuLayout = new RelativeLayout(cont);
                menuLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
                //menuLayout.removeAllViews();
                menuLayout.setBackgroundColor(menu.getColor());
                menuLayout.setBackgroundResource(menu.getBackground());
                //menuLayout.setBackgroundDrawable(menu.getBackground());
                
                //first insert the title
                this.addTextView(menu, menuLayout, menu.getTitle(), false,true);
                
                
                //now add text if existing
                if(menu.getText() != null){
                        this.addTextView(menu, menuLayout, menu.getText(), false,true);
                }
                
                //now add all the items
                MenuSettings settings = menu.getMenuSettings();
                ScrollView sv = new ScrollView(cont);
                
                sv.setFillViewport(true);
                sv.setVisibility(ScrollView.VISIBLE);
                
                LinearLayout ll = new LinearLayout(cont);
                ll.setOrientation(settings.getOrientation());
                ll.setGravity(settings.getGravity());
                for(MenuItem item : menu.getSelectionItems()){
                        if(item.getType() == MenuItem.TEXTBUTTON)
                                this.addTextView(menu, ll, item, true,false);
                        else if(item.getType() == MenuItem.ADJUSTMENT)
                                this.addAdjustmentView(item, menu, ll);
                        else if(item.getType() == MenuItem.SWITCH)
                                this.addSwitchView(item, menu, ll);
                }
                int width = LayoutParams.WRAP_CONTENT;
                int height = LayoutParams.WRAP_CONTENT;
                int mHeight = 0;
                int mWidth = 0;
                if(settings.getHeight() > 0)
                        mHeight = height = ViewUtils.percentHeightToPixels(settings.getHeight());
                if(settings.getWidth() > 0)
                        mWidth =width = ViewUtils.percentWidthToPixels(settings.getWidth());
                RelativeLayout.LayoutParams pr = new RelativeLayout.LayoutParams(width,height);
                //pr.gravity = menu.getMenuSettings().getGravity();

                pr.leftMargin = ViewUtils.percentWidthToPixels(settings.getPosX())-(mWidth/2);
                pr.topMargin = ViewUtils.percentHeightToPixels(settings.getPosY())-(mHeight/2);
                Log.e("asd","Margins" +pr.leftMargin+", "+pr.topMargin+"measuredwidth "+ll.getMeasuredWidth());
                sv.addView(ll, width,height);
                menuLayout.addView(sv, pr);
                menuLayout.invalidate();
                return menuLayout;

                
        }
        
        
        //helper method to create switchviews
        private void addSwitchView(MenuItem item, Menu menu,ViewGroup layout) {

                LinearLayout ll = new LinearLayout(this.cont);
                ll.setOrientation(item.getOrientation());
                ll.setGravity(item.getGravity());
                int width = LayoutParams.FILL_PARENT;
                int height = LayoutParams.WRAP_CONTENT;
                if((menu.getMenuSettings().getWidth() > 0))
                        width = ViewUtils.percentWidthToPixels(menu.getMenuSettings().getWidth());
                
                if((menu.getMenuSettings().getHeight() > 0))
                        height = ViewUtils.percentWidthToPixels(menu.getMenuSettings().getHeight());
                
                if(item.getHeight()>0){
                        height = ViewUtils.percentWidthToPixels(item.getHeight());
                }
                if(item.getWidth()>0)
                        width = ViewUtils.percentWidthToPixels(item.getWidth());

                TextView view = new TextView(cont);
                view.setText(item.getText());
                view.setTextColor(item.getTextColor());
                view.setTypeface(menu.getTypeFace());
                view.setId(item.getId());
                view.setTag(new Integer(item.getIdx()));
                view.setTextSize(ViewUtils.percentHeightToPixels(Math.round(item.getTextSize())));
                StoryButton btn = new StoryButton(cont,item.getTag());
                //btn.setBackgroundColor(menu.getColor());
                btn.setChecked(true);
                //btn.setTextOn("On");
                //btn.setTextOff("Off");
                ll.setGravity(item.getGravity());
                btn.setTextColor(item.getTextColor());
                btn.setTextSize(item.getTextSize());
                btn.setTypeface(menu.getTypeFace());
                if(item.getOrientation() == LinearLayout.HORIZONTAL){
                        ll.addView(view,new LinearLayout.LayoutParams(width/2,LayoutParams.FILL_PARENT));
                        ll.addView(btn, new LinearLayout.LayoutParams(width/2,LayoutParams.FILL_PARENT));
                }
                if(item.getOrientation() == LinearLayout.VERTICAL){
                        ll.addView(view,new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,height/2));
                        ll.addView(btn, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,height/2));
                }
                btn.setOnCheckedChangeListener(listener);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
                int margin = ViewUtils.percentWidthToPixels(2);
                params.setMargins(margin,margin,margin,margin);
                layout.addView(ll, params);
                
        }

        //helper method to create adjustmentviews
        private void addAdjustmentView(MenuItem item,Menu menu, ViewGroup layout) {
                LinearLayout ll = new LinearLayout(this.cont);
                ll.setOrientation(item.getOrientation());
                ll.setGravity(item.getGravity());
                int width = LayoutParams.FILL_PARENT;
                int height = LayoutParams.WRAP_CONTENT;
                if((menu.getMenuSettings().getWidth() > 0))
                        width = ViewUtils.percentWidthToPixels(menu.getMenuSettings().getWidth());
                
                if((menu.getMenuSettings().getHeight() > 0))
                        height = ViewUtils.percentWidthToPixels(menu.getMenuSettings().getHeight());
                
                if(item.getHeight()>0){
                        height = ViewUtils.percentWidthToPixels(item.getHeight());
                }
                if(item.getWidth()>0)
                        width = ViewUtils.percentWidthToPixels(item.getWidth());

                TextView view = new TextView(cont);
                view.setText(item.getText());
                view.setTextColor(item.getTextColor());
                view.setTypeface(menu.getTypeFace());
                view.setId(item.getId());
                view.setTag(new Integer(item.getIdx()));
                view.setTextSize(ViewUtils.percentHeightToPixels(Math.round(item.getTextSize())));
                StoryBar bar = new StoryBar(this.cont,item.getTag());
                AudioManager am = (AudioManager) cont.getSystemService(Context.AUDIO_SERVICE);
                int maxVolume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
                bar.setMax(maxVolume);
                //bar.setBackgroundColor(menu.getColor());
                if(item.getOrientation() == LinearLayout.HORIZONTAL){
                        ll.addView(view,new LinearLayout.LayoutParams(width/2,LayoutParams.FILL_PARENT));
                        ll.addView(bar, new LinearLayout.LayoutParams(width/2,LayoutParams.FILL_PARENT));
                }
                if(item.getOrientation() == LinearLayout.VERTICAL){
                        ll.addView(view,new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,height/2));
                        ll.addView(bar, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,height/2));
                }
                bar.setOnSeekBarChangeListener(listener);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
                int margin = ViewUtils.percentWidthToPixels(2);
                params.setMargins(margin,margin,margin,margin);
                layout.addView(ll, params);
                
        }

        //helper method to create textviews
        private void addTextView(Menu menu, ViewGroup layout, MenuItem item, boolean addListener,boolean relative){
                TextView view = new TextView(cont);
                view.setClickable(true);
                view.setText(item.getText());
                view.setTextColor(item.getTextColor());
                view.setTypeface(menu.getTypeFace());
                view.setId(item.getId());
                view.setTag(new Integer(item.getIdx()));
                view.setTextSize(ViewUtils.percentHeightToPixels(Math.round(item.getTextSize())));
                view.setGravity(item.getGravity());
                view.setTag(item.getIdx());
                Log.e("TAG","Adding view with text "+item.getText()+" color "+item.getTextColor()+" textsize "+item.getTextSize()+" gravity "+item.getGravity());
                
                int pHeight = LayoutParams.WRAP_CONTENT;
                int pWidth = LayoutParams.WRAP_CONTENT;
                int mHeight = 0;
                int mWidth = 0;
                
                if((!relative) && (menu.getMenuSettings().getWidth() > 0))
                        pWidth = mWidth = ViewUtils.percentWidthToPixels(menu.getMenuSettings().getWidth());
                
                if((!relative) && (menu.getMenuSettings().getHeight() > 0))
                        pHeight = mHeight = ViewUtils.percentWidthToPixels(menu.getMenuSettings().getHeight());
                
                if(item.getHeight()>0){
                        pHeight = mHeight = ViewUtils.percentWidthToPixels(item.getHeight());
                }
                if(item.getWidth()>0)
                        pWidth = mWidth = ViewUtils.percentWidthToPixels(item.getWidth());

                if(relative){
                        RelativeLayout.LayoutParams parameters = new RelativeLayout.LayoutParams(pWidth, pHeight);
                        parameters.leftMargin = ViewUtils.percentWidthToPixels(item.getPosX())-(mWidth/2);
                        parameters.topMargin = ViewUtils.percentHeightToPixels(item.getPosY())-(mHeight/2);
                        layout.addView(view, parameters);
                }
                else{
                        LinearLayout.LayoutParams parameters = new LinearLayout.LayoutParams(pWidth, pHeight);
                        Log.e("tst","H:"+pHeight+"W:"+pWidth);
                        view.setGravity(menu.getMenuSettings().getGravity());
                        layout.addView(view,parameters);
                                
                }
                if(addListener){
                        
                        view.setOnTouchListener(listener);
                }
        }

        
        

}
